<?php
/**
 * User: zwh
 * Date: 2016-12-29 10:18:15
 */

class Receivable extends Eloquent
{
    protected $primaryKey = 'receivable_id';
    protected $table = "receivable";
    public static $pay_type=array(
        "1"=>"线下支付现金",
        "2"=>"支付宝",
        "3"=>"微信",
        "7"=>"刷卡",
        "8"=>"余额支付",
        '11'=>'线下支票',
        '15'=>'银行汇款',
        '20'=>'pos支付',
    );

    public static $pay_type_source=array(
        "1"=>array(1,2),
        "2"=>array(1,2),
        "3"=>array(1,2),
        "7"=>array(1,2),
        "8"=>array(1),
        '11'=>array(1),
        '15'=>array(1),
        '20'=>array(2),
    );
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-7-30 14:44:47
     * @Description: 执行收款单
     */
    /**
     * @Author:      lyt
     * @DateTime:  2017-09-14 11:00:22
     * @Description: 添加符合支付 改方法废弃
     */
    // public static function implement($receivable,$receivable_detail,$sc_user,$pay_type){
    //     return DB::transaction(function () use ($receivable,$receivable_detail,$sc_user,$pay_type) {
    //         $response = array('code' => 0, 'msg' => 'ok');
    //         $receivable= Receivable::where("receivable_id",$receivable->receivable_id)->lockForUpdate()->first();
    //         if(is_null($receivable)||$receivable->verify_status!=0||$receivable->status!=1){
    //             $response = Lang::get("scs_error.receivable_error_100001");
    //             break;
    //         }

    //         // $tran_no = date('Ymd',time()).Common::generateCode();       
    //         $tran_no = date('YmdHis',time()).Common::generateCode(8);
    //         $user = Users::find($receivable->company_id);
    //         $account = UserAccount::where('user_id',$user->id)->first();
    //         $order_amount=$receivable->amount;

    //         $rp=new ReceivablePay();
    //         $rp->receivable_id=$receivable->receivable_id;
    //         $rp->pay_type=$pay_type;
    //         $rp->tran_no=$tran_no;
    //         $rp->save();

    //         if($receivable->type==2){//如果是
    //             $ba=BeginArrear::where("shop_id",$sc_user->shop_id)->where("company_id",$receivable->company_id)->lockForUpdate()->first();
    //             if(is_null($ba)||$receivable->amount>$ba->current_amount){//说明抹除的欠款 大于剩余的欠款总额
    //                 DB::rollback();
    //                 return $response = Lang::get("scs_error.begin_receive_error_90017");
    //             }
    //             if($pay_type== 1||$pay_type== 7||$pay_type==11||$pay_type==15){//现金 或者银行卡支付 支票汇款 计入个人的
    //                 //记录用户充值 不记录充值记录  只记录流水
    //                 Account::record($user->id,0,3,$tran_no,1,118,$order_amount,'期初收回收入,收回单号:'.$receivable->receivable_id);
    //                 //记录用户支出
    //                 Account::record_base('1',$tran_no,1,118,$order_amount,'期初收回收入,收回单号:'.$receivable->receivable_id);
    //             }
    //             if($pay_type == 8){//个人余额 暂时不判断 因为个人 余额现在都为空 新版本判断用户余额 个人用用户 授信额为0        
    //                 $uad_balance=UserAccountDeatails::where("user_id",$user->id)->whereIn("acount_type",array(1,2))->sum("balance");      
    //                 if($uad_balance >= $order_amount){
    //                     $arr=AcountUserRecharge::getUseRecharge($receivable->shop_id,$user->id,$order_amount,2,"asc");
    //                     if(sizeof($arr)>0){
    //                         foreach ($arr as $_arr) {
    //                         //记录了用户的充值消费记录
    //                         $aurs=new AcountUserRechargeSpend;
    //                         $aurs->aurr_id=$_arr->id;
    //                         $aurs->user_id=$user->id;
    //                         $aurs->buss_id=$rp->id;
    //                         $aurs->type=7;//期初收回支出
    //                         $aurs->amount=$_arr->user_amount;
    //                         $aurs->status=1;
    //                         $aurs->tran_no=$tran_no;
    //                         $aurs->shop_id=$receivable->shop_id;
    //                         $aurs->implement_time=date("Y-m-d H:i:s",time());
    //                         $aurs->save();
    //                         //更改充值记录消费的记录
    //                         AcountUserRecharge::where('id',$_arr->id)->update(array('current_amount'=> $_arr->current_amount -$_arr->user_amount,));
    //                         //记录个人 各种账户余额
    //                         Account::record($user->id,$_arr->shop_id,$_arr->acount_type,$tran_no,2,218,$_arr->user_amount,'期初欠款支出,收回单号:'.$receivable->receivable_id);
    //                         //记录个个人虚拟账户收入
    //                         Account::record($user->id,0,3,$tran_no,1,118,$_arr->user_amount,'期初收回收入,收回单号:'.$receivable->receivable_id);
    //                         //记录公司账户收入
    //                         $base_acount_num=$_arr->acount_type==1?1:3;//如果用的现金账户 计入企业现金帐 如果是虚拟充值记录
    //                         Account::record_base($base_acount_num,$tran_no,1,118,$_arr->user_amount,'期初收回收入,收回单号:'.$receivable->receivable_id);
    //                     }
    //                 }else{
    //                     DB::rollback();
    //                     $response = Lang::get("scs_error.order_user_notpay_40039");
    //                     return $response;
    //                 }                  
    //                 }else{
    //                     DB::rollback();
    //                     $response = Lang::get("scs_error.order_user_notpay_40039");
    //                     return $response;
    //                 }
    //             }

    //             $ba->current_amount=$ba->current_amount-$receivable->amount;
    //             $ba->save();
    //             // $receivable->verify_by=$sc_user->id;
    //             // $receivable->verify_time=date('Y-m-d H:i:s',time());
    //             // $receivable->verify_status=1;
    //             // $receivable->save();

    //         }else{
    //             /**
    //              * @Author:      lyt
    //              * @DateTime:  2017-05-19 14:14:47
    //              * @Description: 5月 19更换记账逻辑
    //              */
    //             // foreach($receivable_detail as $rd){
    //             //     $time=date('Y-m-d H:i:s',time());
    //             //     if($rd->return_type==0){
    //             //         $opr=OrderPayRecord::leftJoin('orders','orders.order_id','=','order_pay_record.order_id')->where('order_pay_record.pay_type',6)->where('order_pay_record.order_id',$rd->order_id)->where('order_pay_record.status',1)->select('order_pay_record.amount','orders.pay_user_id','orders.verify_status')->first();
    //             //         if($opr->verify_status==1){
    //             //             return $response = Lang::get("scs_error.receivable_error_100002");
    //             //         }
    //             //         $ro=ReturnOrder::leftJoin('return_order_record','return_order_record.return_order_id', '=' ,'return_order.id')
    //             //         ->where('return_order_record.pay_type',6)
    //             //         ->where("return_order.status",1)
    //             //         ->where('return_order.order_id',$rd->order_id)
    //             //         ->first();
    //             //         //如有退货订单，恢复的额度需要减去退货金额
    //             //         if(!is_null($ro)){
    //             //             $amount=$opr->amount-$ro->amount;
    //             //         }else{
    //             //             $amount=$opr->amount;
    //             //         }
    //             //         $tran_no = date('Ymd',time()).Common::generateCode();
    //             //         //记录用户收支
    //             //         Account::record($receivable->company_id,$tran_no,1,'0104',$amount,'应收收回额度恢复');
    //             //         //记录订单账务关系
    //             //         OrderAccount::insert(array('order_id' => $rd->order_id,'tran_no' => $tran_no,));
    //             //         Orders::where('order_id',$rd->order_id)->update(array('verify_time'=>$time,'verify_status'=>1));
    //             //     }
    //             //     if($rd->return_type==1){
    //             //         $ro=ReturnOrder::find($rd->order_id);
    //             //         if($ro->verify_status==1){
    //             //             return $response = Lang::get("scs_error.receivable_error_100002");
    //             //         }
    //             //         ReturnOrder::where('id',$rd->order_id)->update(array('verify_status'=>1,'verify_time'=>$time));
    //             //     }
    //             //     $receivable->verify_by=$sc_user->id;
    //             //     $receivable->verify_time=$time;
    //             //     $receivable->verify_status=1;
    //             //     $receivable->save();
    //             // }
                

    //             if($pay_type== 1||$pay_type== 7||$pay_type==11||$pay_type==15){//现金 或者银行卡支付 支票汇款 计入个人的
    //                 //记录用户充值 不记录充值记录  只记录流水
    //                 Account::record($user->id,0,3,$tran_no,1,114,$order_amount,'订单收回收入,收回单号:'.$receivable->receivable_id);
    //                 //记录用户支出
    //                 Account::record_base('1',$tran_no,1,114,$order_amount,'订单收回收入,收回单号:'.$receivable->receivable_id);
    //             }
    //             if($pay_type == 8){//个人余额 暂时不判断 因为个人 余额现在都为空 新版本判断用户余额 个人用用户 授信额为0        
    //                 $uad_balance=UserAccountDeatails::where("user_id",$user->id)->whereIn("acount_type",array(1,2))->sum("balance");      
    //                 if($uad_balance >= $order_amount){
    //                     $arr=AcountUserRecharge::getUseRecharge($receivable->shop_id,$user->id,$order_amount,2,'asc');
    //                     if(sizeof($arr)>0){
    //                         foreach ($arr as $_arr) {
    //                         //记录了用户的充值消费记录
    //                         $aurs=new AcountUserRechargeSpend;
    //                         $aurs->aurr_id=$_arr->id;
    //                         $aurs->user_id=$user->id;
    //                         $aurs->buss_id=$rp->id;
    //                         $aurs->type=5;//订单收回消费
    //                         $aurs->amount=$_arr->user_amount;
    //                         $aurs->status=1;
    //                         $aurs->tran_no=$tran_no;
    //                         $aurs->shop_id=$receivable->shop_id;
    //                         $aurs->implement_time=date("Y-m-d H:i:s",time());
    //                         $aurs->save();
    //                         //更改充值记录消费的记录
    //                         AcountUserRecharge::where('id',$_arr->id)->update(array('current_amount'=> $_arr->current_amount -$_arr->user_amount,));
    //                         //记录个人 各种账户余额
    //                         Account::record($user->id,$_arr->shop_id,$_arr->acount_type,$tran_no,2,214,$_arr->user_amount,'订单收回支出,收回单号:'.$receivable->receivable_id);
    //                         // 记录个个人虚拟账户收入
    //                         Account::record($user->id,0,3,$tran_no,1,114,$_arr->user_amount,'订单收回收入,收回单号:'.$receivable->receivable_id);
    //                         //记录公司账户收入
    //                         $base_acount_num=$_arr->acount_type==1?1:3;//如果用的现金账户 计入企业现金帐 如果是虚拟充值记录
    //                         Account::record_base($base_acount_num,$tran_no,1,114,$_arr->user_amount,'订单收回收入,收回单号:'.$receivable->receivable_id);
    //                     }
    //                 }else{
    //                     DB::rollback();
    //                     $response = Lang::get("scs_error.order_user_notpay_40039");
    //                     return $response;
    //                 }                  
    //                 }else{
    //                     DB::rollback();
    //                     $response = Lang::get("scs_error.order_user_notpay_40039");
    //                     return $response;
    //                 }
    //             }
    //             $time=date('Y-m-d H:i:s',time());
    //             foreach($receivable_detail as $_rd){
    //                 if($_rd->return_type==0){                    
    //                     if($_rd->order_type==0){
    //                         Orders::where('order_id',$_rd->order_id)->update(array('verify_time'=>$time,'verify_status'=>1));
    //                     }else if($_rd->order_type==1){
    //                         CarWashOrder::where('order_id',$_rd->order_id)->update(array('verify_status'=>1,'verify_time'=>$time));           
    //                     }else{
    //                         ServiceCardOrder::where('order_id',$_rd->order_id)->update(array('verify_status'=>1,'verify_time'=>$time)); 
    //                     }   
    //                 }else{
    //                     if($_rd->order_type==0){
    //                         ReturnOrder::where('id',$_rd->order_id)->update(array('verify_status'=>1,'verify_time'=>$time));
    //                     }else{
    //                         ReturnCarwashOrder::where('id',$_rd->order_id)->update(array('verify_status'=>1,'verify_time'=>$time));
    //                     } 
                        
    //                 }
    //             }
                
    //         }    
    //         $receivable->pay_type=$pay_type;
    //         $receivable->tran_no=$tran_no;
    //         $receivable->verify_by=$sc_user->id;
    //         $receivable->verify_time=date('Y-m-d H:i:s',time());
    //         $receivable->verify_status=1;
    //         $receivable->save();        
    //         return $response;
    //     });
    // }


    /**
     * @Author:      lyt
     * @DateTime:  2017-09-13 16:33:45
     * @Description: 收款单的支付接口 
     *方法内部不对传参验证 请保证传参正确
     */
    public static function pay($receivable_id,$pay_type,$pay_amount,$sc_user_id,$driver_id,$source){
      return DB::transaction(function () use ($receivable_id,$pay_type,$pay_amount,$sc_user_id,$driver_id,$source) {
        $receivable = Receivable::where("receivable_id",$receivable_id)->lockForUpdate()->first();
        if($receivable->verify_status==1){//是否已结算
          DB::rollback();
          $response = Lang::get("scs_error.receivable_error_100006");
          return $response;
        }
        $temp_pay=ReceivablePay::where("receivable_id",$receivable_id)->whereIn("status",array(1,5,7))->where("pay_type",$pay_type)->first();
        if($temp_pay){
          DB::rollback();
          $response = Lang::get("scs_error.receivable_pay_type_error_100008");
          return $response;
        }
        $order_amount=$pay_amount*100;
        $pay_amount=ReceivablePay::where("receivable_id",$receivable_id)->where("status",1)->sum("amount");
        $temp_amount=ReceivablePay::where("receivable_id",$receivable_id)->whereIn("status",array(0,5,7))->sum("amount");
        if(bcsub($receivable->amount, $pay_amount,0)<($order_amount+$temp_amount)){
          DB::rollback();
          $response = Lang::get("scs_error.receivable_pay_type_error_100009");
          return $response;
        }
        $user = Users::find($receivable->company_id);        
        $tran_no = date('YmdHis',time()).Common::generateCode(8);           

        $rp=new ReceivablePay();
        $rp->receivable_id=$receivable->receivable_id;
        $rp->pay_type=$pay_type;
        $rp->tran_no=$tran_no;
        $rp->status=1;
        $rp->sources=1;
        $rp->amount=$order_amount;
        $rp->created_by=$sc_user_id;
        $rp->implement_time=Common::currentTimeString();
        $rp->save();

        if($receivable->type==2){//如果是
            $ba=BeginArrear::where("shop_id",$receivable->shop_id)->where("company_id",$receivable->company_id)->lockForUpdate()->first();
            if(is_null($ba)||$order_amount>$ba->current_amount){//说明抹除的欠款 大于剩余的欠款总额
                DB::rollback();
                return $response = Lang::get("scs_error.begin_receive_error_90017");
            }
            if($source==1){
                if($pay_type== 1||$pay_type== 2||$pay_type== 3||$pay_type== 7||$pay_type==11||$pay_type==15){//现金 或者银行卡支付 支票汇款 计入个人的
                    //记录用户充值 不记录充值记录  只记录流水
                    Account::record($user->id,0,3,$tran_no,1,118,$order_amount,'期初收回收入,收回单号:'.$receivable->receivable_id);
                    //记录用户支出
                    Account::record_base('1',$tran_no,1,118,$order_amount,'期初收回收入,收回单号:'.$receivable->receivable_id);
                }
                if($pay_type == 8){//个人余额 暂时不判断 因为个人 余额现在都为空 新版本判断用户余额 个人用用户 授信额为0        
                    $uad_balance=UserAccountDeatails::where("user_id",$user->id)->whereIn("acount_type",array(1,2))->sum("balance");      
                    if($uad_balance >= $order_amount){
                        $arr=AcountUserRecharge::getUseRecharge($receivable->shop_id,$user->id,$order_amount,2,"asc");
                        if(sizeof($arr)>0){
                            foreach ($arr as $_arr) {
                            //记录了用户的充值消费记录
                            $aurs=new AcountUserRechargeSpend;
                            $aurs->aurr_id=$_arr->id;
                            $aurs->user_id=$user->id;
                            $aurs->buss_id=$rp->id;
                            $aurs->type=7;//期初收回支出
                            $aurs->amount=$_arr->user_amount;
                            $aurs->status=1;
                            $aurs->tran_no=$tran_no;
                            $aurs->shop_id=$receivable->shop_id;
                            $aurs->implement_time=date("Y-m-d H:i:s",time());
                            $aurs->save();
                            //更改充值记录消费的记录
                            AcountUserRecharge::where('id',$_arr->id)->update(array('current_amount'=> $_arr->current_amount -$_arr->user_amount,));
                            //记录个人 各种账户余额
                            Account::record($user->id,$_arr->shop_id,$_arr->acount_type,$tran_no,2,218,$_arr->user_amount,'期初欠款支出,收回单号:'.$receivable->receivable_id);
                            //记录个个人虚拟账户收入
                            Account::record($user->id,0,3,$tran_no,1,118,$_arr->user_amount,'期初收回收入,收回单号:'.$receivable->receivable_id);
                            //记录公司账户收入
                            $base_acount_num=$_arr->acount_type==1?1:3;//如果用的现金账户 计入企业现金帐 如果是虚拟充值记录
                            Account::record_base($base_acount_num,$tran_no,1,118,$_arr->user_amount,'期初收回收入,收回单号:'.$receivable->receivable_id);
                        }
                    }else{
                        DB::rollback();
                        $response = Lang::get("scs_error.order_user_notpay_40039");
                        return $response;
                    }                  
                    }else{
                        DB::rollback();
                        $response = Lang::get("scs_error.order_user_notpay_40039");
                        return $response;
                    }
                }
            }else{//说明是pos支付                
                //支付设备
                $mpd=MaintenancePosDevice::where("id",$driver_id)->where('shop_id',$receivable->shop_id)->where('status',1)->first();
                if(is_null($mpd)){
                  DB::rollback();
                  $response = Lang::get("scs_error.order_pay_driver_error_40116");
                  return $response;
                }
                $call_scan=($pay_type==20?1:0); 
                $temp_pay_type=($pay_type==20?0:$pay_type);
                //调用智能pos 支付
                $w=WposRecord::prepareOrder(4,$receivable_id,$rp->id,$order_amount,$user->id,$receivable->shop_id,$mpd->device_no,$sc_user_id,$temp_pay_type,$call_scan);
                if($w){
                  ReceivablePay::where("id",$rp->id)->update(array("status"=>5,"sources"=>2,"implement_time"=>null,"tran_no"=>''));
                }else{
                  ReceivablePay::where("id",$rp->id)->update(array("status"=>2,"sources"=>2,"implement_time"=>null,"tran_no"=>''));
                  $response = Lang::get("scs_error.order_pay_driver_error_40116");
                  return $response;
                }
                $order_amount=0;//修改支付金额为0 回调产生才算是支付完成                
            }
            //起初首款 付钱 款项就增加 
            $ba->current_amount=$ba->current_amount-$order_amount;
            $ba->save();
        }else{
            if($source==1){
                if($pay_type== 1||$pay_type== 2||$pay_type== 3||$pay_type== 7||$pay_type==11||$pay_type==15){//现金 或者银行卡支付 支票汇款 计入个人的
                    //记录用户充值 不记录充值记录  只记录流水
                    Account::record($user->id,0,3,$tran_no,1,114,$order_amount,'订单收回收入,收回单号:'.$receivable->receivable_id);
                    //记录用户支出
                    Account::record_base('1',$tran_no,1,114,$order_amount,'订单收回收入,收回单号:'.$receivable->receivable_id);
                }
                if($pay_type == 8){//个人余额 暂时不判断 因为个人 余额现在都为空 新版本判断用户余额 个人用用户 授信额为0        
                    $uad_balance=UserAccountDeatails::where("user_id",$user->id)->whereIn("acount_type",array(1,2))->sum("balance");      
                    if($uad_balance >= $order_amount){
                        $arr=AcountUserRecharge::getUseRecharge($receivable->shop_id,$user->id,$order_amount,2,'asc');
                        if(sizeof($arr)>0){
                            foreach ($arr as $_arr) {
                            //记录了用户的充值消费记录
                            $aurs=new AcountUserRechargeSpend;
                            $aurs->aurr_id=$_arr->id;
                            $aurs->user_id=$user->id;
                            $aurs->buss_id=$rp->id;
                            $aurs->type=5;//订单收回消费
                            $aurs->amount=$_arr->user_amount;
                            $aurs->status=1;
                            $aurs->tran_no=$tran_no;
                            $aurs->shop_id=$receivable->shop_id;
                            $aurs->implement_time=date("Y-m-d H:i:s",time());
                            $aurs->save();
                            //更改充值记录消费的记录
                            AcountUserRecharge::where('id',$_arr->id)->update(array('current_amount'=> $_arr->current_amount -$_arr->user_amount,));
                            //记录个人 各种账户余额
                            Account::record($user->id,$_arr->shop_id,$_arr->acount_type,$tran_no,2,214,$_arr->user_amount,'订单收回支出,收回单号:'.$receivable->receivable_id);
                            // 记录个个人虚拟账户收入
                            Account::record($user->id,0,3,$tran_no,1,114,$_arr->user_amount,'订单收回收入,收回单号:'.$receivable->receivable_id);
                            //记录公司账户收入
                            $base_acount_num=$_arr->acount_type==1?1:3;//如果用的现金账户 计入企业现金帐 如果是虚拟充值记录
                            Account::record_base($base_acount_num,$tran_no,1,114,$_arr->user_amount,'订单收回收入,收回单号:'.$receivable->receivable_id);
                        }
                    }else{
                        DB::rollback();
                        $response = Lang::get("scs_error.order_user_notpay_40039");
                        return $response;
                    }                  
                    }else{
                        DB::rollback();
                        $response = Lang::get("scs_error.order_user_notpay_40039");
                        return $response;
                    }
                }
            }else{                
                //支付设备
                $mpd=MaintenancePosDevice::where("id",$driver_id)->where('shop_id',$receivable->shop_id)->where('status',1)->first();
                if(is_null($mpd)){
                  DB::rollback();
                  $response = Lang::get("scs_error.order_pay_driver_error_40116");
                  return $response;
                }
                //调用智能pos 支付
                $call_scan=($pay_type==20?1:0); 
                $temp_pay_type=($pay_type==20?0:$pay_type);
                $w=WposRecord::prepareOrder(4,$receivable_id,$rp->id,$order_amount,$user->id,$receivable->shop_id,$mpd->device_no,$sc_user_id,$temp_pay_type,$call_scan);
                if($w){
                  ReceivablePay::where("id",$rp->id)->update(array("status"=>5,"sources"=>2,"implement_time"=>null,"tran_no"=>''));
                }else{
                  ReceivablePay::where("id",$rp->id)->update(array("status"=>2,"sources"=>2,"implement_time"=>null,"tran_no"=>''));
                  $response = Lang::get("scs_error.order_pay_driver_error_40116");
                  return $response;
                }
                $order_amount=0;//修改支付金额为0 回调产生才算是支付完成                
            }
        }  
        $op_arr=ReceivablePay::where("receivable_id",$receivable->receivable_id)->whereIn("status",array(0,1,5))->get();
        $receivable->pay_type=sizeof($op_arr)==1?$pay_type:9;
        $receivable->save();
        return array('code' => 0, 'msg' => 'ok', 'result' => array("receivable_id"=>$receivable->receivable_id));
      });

    }
     /**
      * @Author:      lyt
      * @DateTime:  2017-09-13 17:50:48
      * @Description: 用于撤销一个收款单支付记录
      */
    public static function cancel($receivable_id,$op_id,$sc_user_id){
      return DB::transaction(function () use ($receivable_id,$op_id,$sc_user_id) {
        $receivable = Receivable::where("receivable_id",$receivable_id)->lockForUpdate()->first();        
        $op=ReceivablePay::find($op_id);

        $tran_no = date('YmdHis',time()).Common::generateCode(8);
        //验证完成订单支付
        $rpu=new ReceivablePayUndo;
        $rpu->rpr_id=$op_id;
        $rpu->receivable_id=$receivable_id;
        $rpu->pay_type=$op->pay_type;
        $rpu->amount=$op->amount;
        $rpu->status=1;
        $rpu->created_by=$sc_user_id;
        $rpu->tran_no=$tran_no;
        $rpu->implement_time=Common::currentTimeString();
        $rpu->save();
        if($op->sources==2){//如果是调用pos 
            $wr=WposRecord::where("type",4)->where("buss_id",$op->id)->first();         
            $p_r=Pos::statusOrder($wr->id);
            if($p_r->status==0){
                if($p_r->data->trade_status=="REFUND"&&$p_r->data->refund_amount==$op->amount){
                    ReceivablePayUndo::where("id",$rpu->id)->update(array("status"=>1,"sources"=>2,"tran_no"=>$tran_no,"implement_time"=>Common::currentTimeString()));
                    $wr->trade_status="REFUND";
                    $wr->pay_info=$p_r->data->pay_info;
                    $wr->return_amont=$op->amount;
                    $wr->save();
                    if($receivable->type==2){//如果是起初回收
                        $ba=BeginArrear::where("shop_id",$receivable->shop_id)->where("company_id",$receivable->company_id)->lockForUpdate()->first();
                        Account::record($receivable->company_id,0,3,$tran_no,2,225,$op->amount,'期初回收撤销支出,收回单号:'.$receivable->receivable_id);
                        //记录用户收入
                        Account::record_base('1',$tran_no,2,225,$op->amount,'期初回收撤销支出,收回单号:'.$receivable->receivable_id);
                        $ba->current_amount=$ba->current_amount+$op->amount;
                        $ba->save();
                    }else{//如果是订单回收
                        //记录用户充值 不记录充值记录  只记录流水
                        Account::record($receivable->company_id,0,3,$tran_no,2,226,$op->amount,'订单回收撤销支出,收回单号:'.$receivable->receivable_id);
                        //记录用户支出
                        Account::record_base('1',$tran_no,2,226,$op->amount,'订单回收撤销支出,收回单号:'.$receivable->receivable_id);
                    }
                    $op->status=6;
                    $op->save();
                    return array('code' => 0, 'msg' => 'ok', 'result' => array("receivable_id"=>$receivable->receivable_id));
                }
            }           
            $p_r=Pos::refundOrder($wr->device_en,$wr->cashier_trade_no);
            if($p_r->status==0){
                ReceivablePayUndo::where("id",$rpu->id)->update(array("status"=>0,"sources"=>2,"tran_no"=>"","implement_time"=>null));
                ReceivablePay::where("id",$op_id)->update(array("status"=>7));
            }else{
                DB::rollback();
                $response=Lang::get("scs_error.user_not_car_cancel_error_40117");
                $response["msg"]=$response["msg"].$p_r->info;
                return $response;
            }
        }else{
            switch ($op->pay_type) {
                case 1://银行卡
                case 2://支付宝
                case 3://微信
                case 7://现金
                case 11://线下支票
                case 15://线下支票
                    if($receivable->type==2){//如果是起初回收
                        $ba=BeginArrear::where("shop_id",$receivable->shop_id)->where("company_id",$receivable->company_id)->lockForUpdate()->first();
                        Account::record($receivable->company_id,0,3,$tran_no,2,225,$op->amount,'期初回收撤销支出,收回单号:'.$receivable->receivable_id);
                        //记录用户收入
                        Account::record_base('1',$tran_no,2,225,$op->amount,'期初回收撤销支出,收回单号:'.$receivable->receivable_id);
                        $ba->current_amount=$ba->current_amount+$op->amount;
                        $ba->save();
                    }else{//如果是订单回收
                        //记录用户充值 不记录充值记录  只记录流水
                        Account::record($receivable->company_id,0,3,$tran_no,2,226,$op->amount,'订单回收撤销支出,收回单号:'.$receivable->receivable_id);
                        //记录用户支出
                        Account::record_base('1',$tran_no,2,226,$op->amount,'订单回收撤销支出,收回单号:'.$receivable->receivable_id);
                    }
                    break;
                case 8://个人余额 暂时不判断 因为个人 余额现在都为空 新版本判断用户余额
                    $type=$receivable->type==2?7:5;
                    $arr=AcountUserRechargeSpend::getUseRecharge($op->id,$op->amount,$type);
                        if(sizeof($arr)>0){
                            foreach ($arr as $_arr) {
                                //记录了用户消费退款记录
                                $aurs=new AcountUserRechargeSpendRe;
                                $aurs->aurr_id=$_arr->aurr_id;
                                $aurs->user_id=$receivable->company_id;//谁使用
                                $aurs->aurr_sp_id=$_arr->id;                      
                                $aurs->buss_id=$rpu->id;
                                $aurs->type=6;//应收收回撤销支付退款 类型
                                $aurs->amount=$_arr->user_amount;
                                $aurs->status=1;
                                $aurs->tran_no=$tran_no;
                                $aurs->shop_id=$receivable->shop_id;
                                $aurs->implement_time=date("Y-m-d H:i:s",time());
                                $aurs->save();
                                //更改充值记录
                                DB::update('update acount_user_recharge_record set current_amount = current_amount+? where id = ?', array($_arr->user_amount,$_arr->aurr_id));
                                //更改充值记录消费记录
                                AcountUserRechargeSpend::where('id',$_arr->id)->update(array('return_amount'=> $_arr->return_amount +$_arr->user_amount,));
                                //记录个人 各种账户余额
                                $sac=AcountUserRecharge::find($_arr->aurr_id);
                                if($receivable->type==2){//如果是起初回收
                                    $ba=BeginArrear::where("shop_id",$receivable->shop_id)->where("company_id",$receivable->company_id)->lockForUpdate()->first();
                                    //支付记录账户收入
                                    Account::record($receivable->company_id,$sac->shop_id,$_arr->acount_type,$tran_no,1,125,$_arr->user_amount,'期初回收撤销收入,收回单号:'.$receivable->receivable_id);
                                    //虚拟账户指出
                                    Account::record($receivable->company_id,0,3,$tran_no,2,225,$_arr->user_amount,'期初回收撤销支出,收回单号:'.$receivable->receivable_id);
                                    //企业支出
                                    $base_acount_num=$_arr->acount_type==1?1:3;//如果用的现金账户 计入企业现金帐 如果是虚拟充值记录
                                    Account::record_base($base_acount_num,$tran_no,2,225,$_arr->user_amount,'期初回收撤销支出,收回单号:'.$receivable->receivable_id);
                                    $ba->current_amount=$ba->current_amount+$_arr->user_amount;
                                    $ba->save();
                                }else{//如果是订单回收
                                    //支付记录账户收入
                                    Account::record($receivable->company_id,$sac->shop_id,$_arr->acount_type,$tran_no,1,126,$_arr->user_amount,'订单收回撤销收入,收回单号:'.$receivable->receivable_id);
                                    //虚拟账户指出
                                    Account::record($receivable->company_id,0,3,$tran_no,2,226,$_arr->user_amount,'订单回收撤销支出,收回单号:'.$receivable->receivable_id);
                                    //企业支出
                                    $base_acount_num=$_arr->acount_type==1?1:3;//如果用的现金账户 计入企业现金帐 如果是虚拟充值记录
                                    Account::record_base($base_acount_num,$tran_no,2,226,$_arr->user_amount,'订单回收撤销支出,收回单号:'.$receivable->receivable_id);
                                }
                            }
                        }else{
                            DB::rollback();
                            $response = Lang::get("scs_error.order_user_notpay_40039");
                            return $response;
                        } 
                    break;              
                default:
                    # code...
                    break;
            }
            $op->status=6;
            $op->save();          
        }
        return array('code' => 0, 'msg' => 'ok', 'result' => array("receivable_id"=>$receivable->receivable_id));
      });

    }

    /**
     * @Author:      lyt
     * @DateTime:  2017-06-23 15:15:32
     * @Description: 用于取消一个撤销中的 记录 也就是取消撤销
     */
    public static function cancelCanceIn($receivable_id,$op_id,$sc_user_id){
      return DB::transaction(function () use ($receivable_id,$op_id,$sc_user_id) {
        $tran_no = date('YmdHis',time()).Common::generateCode(8);
        $receivable = Receivable::where("receivable_id",$receivable_id)->lockForUpdate()->first();        
        $op=ReceivablePay::find($op_id); 
        if($op->sources==2){//如果是调用pos 
            $wr=WposRecord::where("type",4)->where("buss_id",$op->id)->first();         
            $p_r=Pos::statusOrder($wr->id);
            if($p_r->status==0){
                if($p_r->data->trade_status=="REFUND"&&$p_r->data->refund_amount==$op->amount){//如果已经撤销 
                    ReceivablePayUndo::where("rpr_id",$op_id)->update(array("status"=>1,"sources"=>2,"tran_no"=>$tran_no,"implement_time"=>Common::currentTimeString()));
                    $wr->trade_status="REFUND";
                    $wr->pay_info=$p_r->data->pay_info;
                    $wr->return_amont=$op->amount;
                    $wr->save();
                    if($receivable->type==2){//如果是起初回收
                        $ba=BeginArrear::where("shop_id",$receivable->shop_id)->where("company_id",$receivable->company_id)->lockForUpdate()->first();
                        Account::record($receivable->company_id,0,3,$tran_no,2,225,$op->amount,'期初回收撤销支出,收回单号:'.$receivable->receivable_id);
                        //记录用户收入
                        Account::record_base('1',$tran_no,2,225,$op->amount,'期初回收撤销支出,收回单号:'.$receivable->receivable_id);
                        $ba->current_amount=$ba->current_amount+$op->amount;
                        $ba->save();
                    }else{//如果是订单回收
                        //记录用户充值 不记录充值记录  只记录流水
                        Account::record($receivable->company_id,0,3,$tran_no,2,226,$op->amount,'订单回收撤销支出,收回单号:'.$receivable->receivable_id);
                        //记录用户支出
                        Account::record_base('1',$tran_no,2,226,$op->amount,'订单回收撤销支出,收回单号:'.$receivable->receivable_id);
                    }
                    $op->status=6;
                    $op->save();
                    return array('code' => 0, 'msg' => 'ok', 'result' => array("receivable_id"=>$receivable->receivable_id));
                }else if($p_r->data->trade_status=="PAY"){//说明订单还没撤销
                    ReceivablePayUndo::where("rpr_id",$op_id)->update(array("status"=>2,"sources"=>2,"tran_no"=>"","implement_time"=>null));
                    ReceivablePay::where("id",$op_id)->update(array("status"=>1));
                    return array('code' => 0, 'msg' => 'ok', 'result' => array("receivable_id"=>$receivable->receivable_id));
                }
            }
        }
        return Lang::get("scs_error.order_pay_error_40122");
      });
    }

    /**
     * @Author:      lyt
     * @DateTime:  2017-09-14 13:31:24
     * @Description: 用于取消一个支付中的记录
     */
    public static function cancelPayIn($receivable_id,$op_id){
      return DB::transaction(function () use ($receivable_id,$op_id) {
        $receivable = Receivable::where("receivable_id",$receivable_id)->lockForUpdate()->first();        
        $op=ReceivablePay::find($op_id);         
        if($op->sources==2){//如果是调用pos 现在只有这一种哦
            $wr=WposRecord::where("type",4)->where("buss_id",$op->id)->first();         
            //查询订单状态
            $p_r=Pos::statusOrder($wr->id);
            if($p_r->status==0){ //订单存在
                if($p_r->data->trade_status=="CLOSED"){//如果交易关闭了
                    $wr->cashier_trade_no=$p_r->data->cashier_trade_no;
                    $wr->mcode=$p_r->data->mcode;
                    $wr->device_en=$p_r->data->device_en;
                    $wr->trade_status=$p_r->data->trade_status;
                    $wr->pay_info=$p_r->data->pay_info;
                    $wr->attach=isset($p_r->data->attach)?$p_r->data->attach:"";
                    $wr->buyer=isset($p_r->data->buyer)?$p_r->data->buyer:"";
                    $wr->thirdSerialNo=isset($p_r->data->thirdSerialNo)?$p_r->data->thirdSerialNo:"";
                    $wr->terminalNo=isset($p_r->data->terminalNo)?$p_r->data->terminalNo:"";
                    $wr->vouchNo=isset($p_r->data->vouchNo)?$p_r->data->vouchNo:"";
                    $wr->batchNo=isset($p_r->data->batchNo)?$p_r->data->batchNo:"";
                    $wr->cardType=isset($p_r->data->cardType)?$p_r->data->cardType:"";
                    $wr->save();                    
                    $op->status=4;
                    $op->save();
                    $result = array('code' => 0, 'msg' => 'ok', 'result' => array("receivable_id"=>$receivable->receivable_id));
                }else{ //交易其他状态
                    $result = Lang::get("scs_error.order_pay_error_40121");
                }
            }elseif($p_r->status==1004){ //订单不存在
                //查询消息推送情况
                $p_seq = Pos::statusPush($wr->seq_no);
                if($p_seq->status == 0){ //查询成功
                    if($p_seq->data->delivery == 1){ //送达
                        //查询订单状态
                        sleep(11);
                        $p_r=Pos::statusOrder($wr->id);
                        if($p_r->status==0){ //订单存在
                            if($p_r->data->trade_status=="CLOSED"){//如果交易关闭了
                                $wr->cashier_trade_no=$p_r->data->cashier_trade_no;
                                $wr->mcode=$p_r->data->mcode;
                                $wr->device_en=$p_r->data->device_en;
                                $wr->trade_status=$p_r->data->trade_status;
                                $wr->pay_info=$p_r->data->pay_info;
                                $wr->attach=isset($p_r->data->attach)?$p_r->data->attach:"";
                                $wr->buyer=isset($p_r->data->buyer)?$p_r->data->buyer:"";
                                $wr->thirdSerialNo=isset($p_r->data->thirdSerialNo)?$p_r->data->thirdSerialNo:"";
                                $wr->terminalNo=isset($p_r->data->terminalNo)?$p_r->data->terminalNo:"";
                                $wr->vouchNo=isset($p_r->data->vouchNo)?$p_r->data->vouchNo:"";
                                $wr->batchNo=isset($p_r->data->batchNo)?$p_r->data->batchNo:"";
                                $wr->cardType=isset($p_r->data->cardType)?$p_r->data->cardType:"";
                                $wr->save();
                                $op->status=4;
                                $op->save();
                               
                                $result = array('code' => 0, 'msg' => 'ok', 'result' => array("receivable_id"=>$receivable->receivable_id));
                            }else{ //交易其他状态
                                $result = Lang::get("scs_error.order_pay_error_40121");
                            }
                        }elseif($p_r->status==1004){//订单不存在
                            //关闭订单
                            $op->status=4;
                            $op->save();
                            $result = array('code' => 0, 'msg' => 'ok', 'result' => array("receivable_id"=>$receivable->receivable_id));
                        }else{
                            $result = Lang::get("scs_error.order_pay_error_40121");
                        }
                    }elseif($p_seq->data->delivery == 0){ //未送达
                        //报警
                        $phones = Config::get('config.notify_phone');
                        $content = '收回单['.$receivable->receivable_id.']取消失败，pos订单不存在且推送未送达，请及时处理。';
                        foreach ($phones as $_phone){
                            Sms::send_message('010',$_phone,$content);
                        }
                        $result = Lang::get("scs_error.order_pay_error_40121");
                    }else{
                        //报警
                        $phones = Config::get('config.notify_phone');
                        $content = '收回单['.$receivable->receivable_id.']取消失败，pos订单不存在且推送状态未知，请及时处理。';
                        foreach ($phones as $_phone){
                            Sms::send_message('010',$_phone,$content);
                        }
                        $result = Lang::get("scs_error.order_pay_error_40121");
                    }
                }else{ //查询不成功
                    //报警
                    $phones = Config::get('config.notify_phone');
                    $content = '收回单['.$receivable->receivable_id.']取消失败，pos订单不存在且推送状态查询失败，请及时处理。';
                    foreach ($phones as $_phone){
                        Sms::send_message('010',$_phone,$content);
                    }
                    $result = Lang::get("scs_error.order_pay_error_40121");
                }
            }else{ //订单其他状态
                $result = Lang::get("scs_error.order_pay_error_40121");
            }
        }
        return $result;
      });
    }
}

